package com.wenwu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsDetail;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsDetailMapper;
import com.wenwu.service.CommonService;

/**
 * @project RelicsProject-service
 * @class CommonServiceImpl.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年5月6日 下午9:53:25
 * @description 公共service层实现类
 */
@Service
public class CommonServiceImpl implements CommonService {
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	@Autowired
	private RelicsDetailMapper detaiMapper;

	public List<RelicsAttachment> selectAttachmentListByKey(Integer key,
			Integer... owns) throws RelicsException {
		try {
			List<Integer> list = new ArrayList<Integer>();
			for (Integer own : owns) {
				list.add(own);
			}
			return attachmentMapper.selectAttachmentListByKey(key, list);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public List<RelicsAttachment> selectAttachmentListByKey(Integer key,
			String tableName, String filedId1, String filedId2, Integer... owns)
			throws RelicsException {
		try {
			List<Integer> list = new ArrayList<Integer>();
			for (Integer own : owns) {
				list.add(own);
			}
			return attachmentMapper.selectAttachmentListByKeyAndTable(key,
					tableName, filedId1, filedId2, list);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public RelicsDetail selectRelicsDetail() throws RelicsException {
		try {
			List<RelicsDetail> list = detaiMapper
					.selectByExampleWithBLOBs(null);
			if (list != null && list.size() > 0) {
				return list.get(0);
			} else
				return null;
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public RelicsDetail addRelicsdetail(RelicsDetail detail)
			throws RelicsException {
		int result=0;
		try {
			if (detail.getDetailId()==null) {
				result=detaiMapper.insertSelective(detail);
			}else{
				result=detaiMapper.updateByPrimaryKeySelective(detail);
			}
			if (result>0) {
				detail.setMsg("保存成功!");
			}else{
				detail.setMsg("保存失败!");
			}
			return detail;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RelicsException("系统异常");
		}
	}
}
